package com.zhuss.core.login;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.zhuss.bean.bo.core.auth.AuthBo;
import com.zhuss.bean.bo.core.login.LoginBo;
import com.zhuss.bean.bo.core.login.LogoutBo;
import com.zhuss.bean.bo.core.login.UpdateBo;
import com.zhuss.bean.bo.core.login.UpdatePicBo;
import com.zhuss.bean.bo.core.login.UpdatePwdBo;
import com.zhuss.bean.bo.core.login.wechat.BindBo;
import com.zhuss.bean.bo.core.login.wechat.InitBo;
import com.zhuss.bean.bo.core.login.wechat.LoginChatBo;
import com.zhuss.bean.bo.core.login.wechat.SendCodeBo;
import com.zhuss.bean.bo.core.login.wechat.UnbindBo;
import com.zhuss.bean.bo.core.user.UpdateUserBo;
import com.zhuss.bean.vo.core.auth.AuthVo;
import com.zhuss.bean.vo.core.login.LoginVo;
import com.zhuss.bean.vo.core.login.SendCodeVo;
import com.zhuss.common.LoginOperateService;
import com.zhuss.common.Processor;
import com.zhuss.exception.WpsException;

/**
 * 登录service（基本操作）
 * @author zhushanshuo
 * @Date 2019-12-13 15:59
 *
 */
@Component("loginService")
public class LoginService implements LoginOperateService<Object, Object> {
	
	
	@Autowired
	@Qualifier("loginProcessor")
	private Processor<LoginVo, LoginBo> loginProcessor;
	
	@Autowired
	@Qualifier("logoutProcessor")
	private Processor<Object, LogoutBo> logoutProcessor;
	
	@Autowired
	@Qualifier("pwdUpdateProcessor")
	private Processor<Object, UpdatePwdBo> pwdUpdateProcessor;
	
	@Autowired
	@Qualifier("picUpdateProcessor")
	private Processor<Object, UpdatePicBo> picUpdateProcessor;
	
	@Autowired
	@Qualifier("userUpdateSimple")
	private Processor<Object, UpdateUserBo> userUpdateSimple;
	
	@Autowired
	@Qualifier("initProcessor")
	private Processor<LoginVo, InitBo> initProcessor;
	
	@Autowired
	@Qualifier("authTreeProcessor")
	private Processor<AuthVo, AuthBo> authTreeProcessor;
	
	
	@Autowired
	@Qualifier("codeSend")
	private Processor<SendCodeVo, SendCodeBo> codeSend;
	
	/**
	 * 登录
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public Object login(Object param) throws WpsException {
		
		LoginBo bo = (LoginBo) param;
		Object result = loginProcessor.process(bo);
		return result;
	}
	
	/**
	 * 登出
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public Object logout(Object param) throws WpsException {
		
		LogoutBo bo = (LogoutBo) param;
		logoutProcessor.process(bo);
		return null;
	}
	
	/**
	 * 修改密码
	 */
	@Transactional(rollbackFor = Exception.class)
	public Object updatePwd(Object param) throws WpsException {
		
		UpdatePwdBo bo = (UpdatePwdBo) param;
		pwdUpdateProcessor.process(bo);
		return null;
	}
	
	/**
	 * 修改头像
	 */
	@Transactional(rollbackFor = Exception.class)
	public Object updatePic(Object param) throws WpsException {
		
		UpdatePicBo bo = (UpdatePicBo) param;
		picUpdateProcessor.process(bo);
		return null;
	}
	
	/**
	 * 修改
	 */
	@Transactional(rollbackFor = Exception.class)
	public Object update(Object param) throws WpsException {
		
		UpdateBo bo = (UpdateBo) param;
		UpdateUserBo userBo = new UpdateUserBo();
		BeanUtils.copyProperties(bo, userBo);
		userBo.setUserId(bo.getConcurrentUserId());
		userUpdateSimple.process(userBo);
		return null;
	}
	
	/**
	 * 初始化
	 */
	public Object menu(Object param) throws WpsException {
		
		AuthBo bo = (AuthBo) param;
		Object result = authTreeProcessor.process(bo);
		return result;
	}
	
	/**
	 * 初始化
	 */
	@Transactional(rollbackFor = Exception.class)
	public Object init(Object param) throws WpsException {
		
		InitBo bo = (InitBo) param;
		Object result = initProcessor.process(bo);
		return result;
	}
	
	/**
	 * 绑定
	 * 登录
	 */
	@Transactional(rollbackFor = Exception.class)
	public Object bind(Object param) throws WpsException {
		
		BindBo bo = (BindBo) param;
		LoginBo loginBo = new LoginBo();
		BeanUtils.copyProperties(bo, loginBo);
		Object result = loginProcessor.process(loginBo);
		return result;
	}
	
	/**
	 * 注册 or
	 * 登录
	 */
	@Transactional(rollbackFor = Exception.class)
	public Object loginChat(Object param) throws WpsException {
		
		LoginChatBo bo = (LoginChatBo) param;
		LoginBo loginBo = new LoginBo();
		BeanUtils.copyProperties(bo, loginBo);
		Object result = loginProcessor.process(loginBo);
		return result;
	}
	
	/**
	 * 验证码
	 */
	@Transactional(rollbackFor = Exception.class)
	public Object sendCode(Object param) throws WpsException {
		
		SendCodeBo bo = (SendCodeBo) param;
		SendCodeVo result = codeSend.process(bo);
		return result;
	}
	
	/**
	 * 解绑
	 * 登出
	 */
	@Transactional(rollbackFor = Exception.class)
	public Object unbind(Object param) throws WpsException {
		
		UnbindBo bo = (UnbindBo) param;
		LogoutBo logoutBo = new LogoutBo();
		BeanUtils.copyProperties(bo, logoutBo);
		logoutProcessor.process(logoutBo);
		return null;
	}
	
}

